<?php

defined('IN_PHPCMS') or exit('No permission resources.');
pc_base::load_app_class('admin', 'admin', 0);

class apply_classes extends admin {

    public function __construct() {
        parent::__construct();
    }

    const str1 = '您报名的 "{name}" 已经通过审核, 请等待工作人员与您联系';
    const str2 = '您报名的 "{name}" 未能通过审核, 请继续关注其他课程信息';
    const str3 = '"{name}老师" 已接受您的预约, 请等待工作人员与您联系';
    const str4 = '"{name}老师" 未接受您的预约, 请继续关注我们其他信息';
    const str5 = '您申请成为导师已通过, 请等待工作人员与您联系';
    const str6 = '您申请成为导师未通过, 请等待工作人员与您联系';

    public function init() {
        $db_apply_class = pc_base::load_model('apply_class_model');

        if ($_GET['status1'] == 0 && $_GET['status2']) {
            $id = (int)$_GET['id'];
            $status2 = (int)$_GET['status2'];

            $db_reminds = pc_base::load_model('reminds_model');
            $db_classmany = pc_base::load_model('classmany_model');

            $r = $db_apply_class->update(['status' => $status2], ['id' => $id, 'status' => 0]);
            if ($r) {
                $r = $db_apply_class->get_one(['id' => $id]);
                $uid = $r['uid'];
                $class_id = $r['a_id'];
                $class_res = $db_classmany->pkhs($class_id);

                if ($status2 == 1) {
                    $str = self::str1;
                } else {
                    $str = self::str2;
                }

                $str = str_replace('{name}', $class_res['title'], $str);

                $info = [
                    'uid' => $uid,
                    'type' => 1,
                    'title' => $str,
                    'small_content' => $str,
                    'ymd' => date('Y-m-d'),
                    'ymdhis' => date('Y-m-d H:i:s'),
                ];

                $db_reminds->insert($info);


                showmessage('操作成功', '/?m=content&c=apply_classes&a=init');
            } else {
                showmessage('操作失败', '/?m=content&c=apply_classes&a=init');
            }
        }

        $where = '1';
        $status = isset($_GET['status']) ? (int)$_GET['status'] : -1;
        if (in_array($status, [0, 1, 2])) {
            $where .= " and `status`='$status'";
        }

        $uid = $_GET['uid'] ? (int)$_GET['uid'] : '';
        if ($uid) {
            $where .= " and uid=$uid";
        }

        $a_id = $_GET['a_id'] ? (int)$_GET['a_id'] : '';
        if ($a_id) {
            $where .= " and a_id=$a_id";
        }

        $datas = $db_apply_class->listinfo($where, 'id desc', $_GET['page']);
        $pages = $db_apply_class->pages;

        $g_url = get_url();

        include $this->admin_tpl('apply_classes_list');
    }

    public function index() {
        $db_apply_class = pc_base::load_model('apply_expert_model');

        if ($_GET['status1'] == 0 && $_GET['status2']) {
            $id = (int)$_GET['id'];
            $status2 = (int)$_GET['status2'];

            $db_reminds = pc_base::load_model('reminds_model');
            $db_classmany = pc_base::load_model('SuperStar_model');

            $r = $db_apply_class->update(['status' => $status2], ['id' => $id, 'status' => 0]);
            if ($r) {
                $r = $db_apply_class->get_one(['id' => $id]);
                $uid = $r['uid'];
                $class_id = $r['a_id'];
                $class_res = $db_classmany->pkhs($class_id);

                if ($status2 == 1) {
                    $str = self::str3;
                } else {
                    $str = self::str4;
                }

                $str = str_replace('{name}', $class_res['title'], $str);

                $info = [
                    'uid' => $uid,
                    'type' => 1,
                    'title' => $str,
                    'small_content' => $str,
                    'ymd' => date('Y-m-d'),
                    'ymdhis' => date('Y-m-d H:i:s'),
                ];

                $db_reminds->insert($info);


                showmessage('操作成功', '/?m=content&c=apply_classes&a=index');
            } else {
                showmessage('操作失败', '/?m=content&c=apply_classes&a=index');
            }
        }

        $where = '1';
        $status = isset($_GET['status']) ? (int)$_GET['status'] : -1;
        if (in_array($status, [0, 1, 2])) {
            $where .= " and `status`='$status'";
        }

        $uid = $_GET['uid'] ? (int)$_GET['uid'] : '';
        if ($uid) {
            $where .= " and uid=$uid";
        }

        $a_id = $_GET['a_id'] ? (int)$_GET['a_id'] : '';
        if ($a_id) {
            $where .= " and a_id=$a_id";
        }
        $db_classmany = pc_base::load_model('SuperStar_model');

        $datas = $db_apply_class->listinfo($where, 'id desc', $_GET['page']);
        foreach ($datas as &$v) {
            $t_name = $db_classmany->get_one('id=' . $v['a_id'], 'title');
            $v['t_name'] = $t_name['title'];
        }
        $pages = $db_apply_class->pages;

        $g_url = get_url();
        //echo $this->admin_tpl('apply_expert_list');exit();
        include $this->admin_tpl('apply_expert_list');
    }

    public function do_teacher() {
        $uid = (int)param::get_cookie('_userid');
        $db_apply_class = pc_base::load_model('apply_teacher_model');
        if ($_GET['status1'] == 0 && $_GET['status2']) {
            $id = (int)$_GET['id'];
            $status2 = (int)$_GET['status2'];

            $db_reminds = pc_base::load_model('reminds_model');
            $db_classmany = pc_base::load_model('SuperStar_model');

            $r = $db_apply_class->update(['status' => $status2], ['id' => $id, 'status' => 0]);
            if ($r) {

                if ($status2 == 1) {
                    $str = self::str5;
                } else {
                    $str = self::str6;
                }
                $uid = $db_apply_class->get_one('id=' . $id, 'uid');


                $info = [
                    'uid' => $uid['uid'],
                    'type' => 1,
                    'title' => $str,
                    'small_content' => $str,
                    'ymd' => date('Y-m-d'),
                    'ymdhis' => date('Y-m-d H:i:s'),
                ];

                $db_reminds->insert($info);


                showmessage('操作成功', '/?m=content&c=apply_classes&a=do_teacher');
            } else {
                showmessage('操作失败', '/?m=content&c=apply_classes&a=do_teacher');
            }
        }
        $where = '1';
        $uid = $_GET['uid'] ? (int)$_GET['uid'] : '';
        if ($uid) {
            $where .= " and uid=$uid";
        }
        $datas = $db_apply_class->listinfo($where, 'id desc', $_GET['page']);
        foreach ($datas as &$v) {
            $v['type'] = str_replace(0, '电话沟通', $v['type']);
            $v['type'] = str_replace(1, '约谈', $v['type']);
            $v['type'] = str_replace(2, '任务委托', $v['type']);
        }

        $pages = $db_apply_class->pages;
        $g_url = get_url();
        include $this->admin_tpl('apply_teacher_list');
    }

    //申请千人计划
    public function do_teachers() {        
        $uid = (int)param::get_cookie('_userid');
        $db_apply_teachers = pc_base::load_model('apply_teachers_model');
        if ($_GET['status1'] == 0 && $_GET['status2']) {
            $id = (int)$_GET['id'];
            $status2 = (int)$_GET['status2'];

            $db_reminds = pc_base::load_model('reminds_model');

            $r = $db_apply_teachers->update(['status' => $status2], ['id' => $id, 'status' => 0]);
            if ($r&&$status2==1) {
                //create recode v9_SuperStar status=1
                $userinfo1 = $db_apply_teachers->pkhs($id);
                $star_info = [
                    'title' => $userinfo1['realname'],
                    'thumb' => $userinfo1['img'],
                    'type' => ','.$userinfo1['types'],
                    'Phone_interviews' => $userinfo1['tel_rmb'].'元',
                    'appointment' => $userinfo1['rmb'].'元',
                    'Task_delegation' => '视情况定',
                    'goodjob' => $userinfo1['good'],
                    'job' => $userinfo1['job'],
                    'con' => $userinfo1['con'],
                    'catid' => 2,
                    'status' => 1,
                    'sysadd' => 1,
                ];
                //print_r($star_info);exit();
                $this->db = pc_base::load_model('content_model');
                $this->db->set_model(22);
                $res = $this->db->add_content($star_info);
                $add_id=$res;
                $SuperStar = pc_base::load_model('SuperStar_model');
                $SuperStar->update('type=",'.$userinfo1['types'].'"','id='.$res);
                if ($userinfo1['con1']!='') {
                    $star_info = [
                        'title' => $userinfo1['con1'],                    
                        'catid' => 13,
                        'status' => 99,
                        'sysadd' => 1,
                        'p_id' => $add_id,
                    ];
                    //print_r($star_info);exit();
                    $this->db = pc_base::load_model('content_model');
                    $this->db->set_model(23);
                    $res = $this->db->add_content($star_info);
                }
                if ($userinfo1['con2']!='') {
                    $star_info = [
                        'title' => $userinfo1['con2'],                    
                        'catid' => 13,
                        'status' => 99,
                        'sysadd' => 1,
                        'p_id' => $add_id,
                    ];
                    //print_r($star_info);exit();
                    $this->db = pc_base::load_model('content_model');
                    $this->db->set_model(23);
                    $res = $this->db->add_content($star_info);
                }

                showmessage('操作成功,移动到 [高见导师] [一审]' . var_export($res, 1), '/?m=content&c=apply_classes&a=do_teachers');
            } else {
                showmessage('申请未通过', '/?m=content&c=apply_classes&a=do_teachers');
            }
        }
        
        $where = '1';
        $realname = $_GET['realname'];
        if ($realname) {
            $where .= " and realname='$realname'";
        }
        $status = $_GET['status'];
        if ($status) {
            $where .= " and status='$status'";
        }
        
        $datas = $db_apply_teachers->listinfo($where, 'id desc', $_GET['page']);
        foreach ($datas as &$v) {
            $v['types'] = str_replace(0, '电话沟通', $v['types']);
            $v['types'] = str_replace(1, '约谈', $v['types']);
            $v['types'] = str_replace(2, '任务委托', $v['types']);
            $v['types'] = substr($v['types'], 0,-1);
        }

        $pages = $db_apply_teachers->pages;
        $g_url = get_url();

        include $this->admin_tpl('apply_teachers_list');
    }

    public function i_can() {
        $id = intval($_GET['id']);
        $i_can = pc_base::load_model('i_can_model');
        $con = $i_can->select('p_id=' . $id);

        $pages = $i_can->pages;
        include $this->admin_tpl('i_can_list');
    }
}

function t_type($a) {
    switch ($a) {
        case 0:
            return '审核中';
            break;
        case 1:
            return '已成功';
            break;
        case 2:
            return '未通过';
            break;
    }
}

function status2cn($a) {
    switch ($a) {
        case 0:
            return '审核中';
            break;
        case 1:
            return '已成功';
            break;
        case 2:
            return '未通过';
            break;
    }
}

function yuyue_type($a) {
    switch ($a) {
        case 0:
            return '电话沟通';
            break;
        case 1:
            return '约谈';
            break;
        case 2:
            return '任务委托';
            break;
    }
}

function jieguo_date($a) {
    switch ($a) {
        case 0:
            return '3天内';
            break;
        case 1:
            return '5天内';
            break;
        case 2:
            return '一周内';
            break;
        case 3:
            return '不限';
            break;
    }
}

function num1($a) {
    switch ($a) {
        case 0:
            return '30分钟';
            break;
        case 1:
            return '1小时';
            break;
        case 2:
            return '1小时30分钟';
            break;
        case 3:
            return '2小时';
            break;
        case 4:
            return '2小时30分钟';
            break;
        case 5:
            return '3小时';
            break;
        case 6:
            return '3小时30分钟';
            break;
        case 7:
            return '4小时';
            break;
    }
}

function num2($a) {
    switch ($a) {
        case 0:
            return '1小时';
            break;
        case 1:
            return '2小时';
            break;
        case 2:
            return '3小时';
            break;
        case 3:
            return '4小时';
            break;
        case 4:
            return '5小时';
            break;
        case 5:
            return '6小时';
            break;
        case 6:
            return '7小时';
            break;
        case 7:
            return '8小时';
            break;
        case 8:
            return '更多';
            break;
    }
}

function num3($a) {
    switch ($a) {
        case 0:
            return '1件';
            break;
        case 1:
            return '2件';
            break;
        case 2:
            return '3件';
            break;
        case 3:
            return '4件';
            break;
        case 4:
            return '5件';
            break;
        case 5:
            return '更多';
            break;
    }
}
